﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy
{
    public partial class KhachHang_GUI : Form
    {
        List<KhachHang_BIZ> list_KH = new List<KhachHang_BIZ>();
        KhachHang_BIZ current_kh = new KhachHang_BIZ();
        bool updated = true;

        public KhachHang_GUI()
        {
            InitializeComponent();
            GridView_KH.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_KH = current_kh.GetAll_KH();
            GridView_KH.DataSource = list_KH;
            Textbox_Disable(true);
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            Textbox_Disable(false);
            Reset();
            updated = false;
        }

        private void GridView_KH_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_KH.Columns[e.ColumnIndex].HeaderText == "Xóa")
            {
                current_kh = (KhachHang_BIZ)GridView_KH.Rows[e.RowIndex].DataBoundItem;
                list_KH.Remove(current_kh);
                current_kh.Delete();
                GridView_KH.DataSource = null;
                GridView_KH.DataSource = list_KH;
            }
            if (GridView_KH.Columns[e.ColumnIndex].HeaderText == "Sửa")
            {
                current_kh = (KhachHang_BIZ)GridView_KH.Rows[e.RowIndex].DataBoundItem;
                Textbox_Disable(false);
            }
        }

        private void GridView_KH_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_KH.SelectedRows.Count > 0)
            {
                current_kh = (KhachHang_BIZ)GridView_KH.SelectedRows[0].DataBoundItem;
                Set_Val();
                Textbox_Disable(true);
            }
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {
                if (!updated)
                {
                    current_kh = new KhachHang_BIZ(-1, txt_MaKH.Text, txt_TenKH.Text, txt_SDT.Text, txt_Email.Text, txt_DiaChi.Text, txt_username.Text, txt_pass.Text);
                    list_KH.Add(current_kh);
                }
                else
                {
                    Get_Val();
                }
                current_kh.Save();
                GridView_KH.DataSource = null;
                GridView_KH.DataSource = list_KH;
                Textbox_Disable(true);
                updated = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi!" + ex.Message);
            }  
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void Textbox_Disable(bool _disable)
        {
            txt_DiaChi.ReadOnly =
            txt_Email.ReadOnly =
            txt_MaKH.ReadOnly =
            txt_pass.ReadOnly =
            txt_SDT.ReadOnly =
            txt_TenKH.ReadOnly =
            txt_username.ReadOnly = _disable;
        }

        void Get_Val()
        {
            current_kh.Diachi = txt_DiaChi.Text;
            current_kh.Email = txt_Email.Text;
            current_kh.MaKH = txt_MaKH.Text;
            current_kh.Pass = txt_pass.Text;
            current_kh.Sdt = txt_SDT.Text;
            current_kh.TenKH = txt_TenKH.Text;
            current_kh.Username = txt_username.Text;
        }

        void Set_Val()
        {
            txt_DiaChi.Text = current_kh.Diachi;
            txt_Email.Text = current_kh.Email;
            txt_MaKH.Text = current_kh.MaKH;
            txt_pass.Text = current_kh.Pass;
            txt_SDT.Text = current_kh.Sdt;
            txt_TenKH.Text = current_kh.TenKH;
            txt_username.Text = current_kh.Username;
        }

        void Reset()
        {
            int id = current_kh.Get_Lasted_ID();
            txt_DiaChi.Text = 
            txt_Email.Text =             
            txt_pass.Text = 
            txt_SDT.Text = 
            txt_TenKH.Text = 
            txt_username.Text = "";
            txt_MaKH.Text = "KH" + (id + 1);
        }
    }
}
